Shader "Custom/ProgressBarCutoff"
{
    Properties
    {
        _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        _FillAmount ("Fill Amount", Range(0, 1)) = 1
        _FillDirection ("Fill Direction", Float) = 0 // 0=水平, 1=垂直
        
        [Header(Segment Dividers)]
        _DividerTex ("Divider Texture", 2D) = "white" {}
        _SegmentCount ("Segment Count", Int) = 1
        _DividerWidth ("Divider Width", Range(0, 0.1)) = 0.02
        _DividerColor ("Divider Color", Color) = (1,1,1,1)
        
        [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }

        Cull Off
        Lighting Off
        ZWrite Off
        Blend One OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0
            #pragma multi_compile_local _ PIXELSNAP_ON
            
            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
                UNITY_VERTEX_OUTPUT_STEREO
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _DividerTex;
            fixed4 _Color;
            fixed4 _DividerColor;
            float _FillAmount;
            float _FillDirection;
            int _SegmentCount;
            float _DividerWidth;

            v2f vert(appdata_t IN)
            {
                v2f OUT;
                
                UNITY_SETUP_INSTANCE_ID(IN);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
                
                OUT.vertex = UnityObjectToClipPos(IN.vertex);
                OUT.texcoord = TRANSFORM_TEX(IN.texcoord, _MainTex);
                OUT.color = IN.color * _Color;
                
                #ifdef PIXELSNAP_ON
                OUT.vertex = UnityPixelSnap(OUT.vertex);
                #endif

                return OUT;
            }

            fixed4 frag(v2f IN) : SV_Target
            {
                float halfDividerWidth = _DividerWidth * 0.5;
                float isVertical = step(0.5, _FillDirection);
                float coord = lerp(IN.texcoord.x, IN.texcoord.y, isVertical);
                
                clip(_FillAmount - coord);
                
                fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
                float segmentMask = step(1.5, _SegmentCount);
                float segmentWidth = 1.0 / max(_SegmentCount, 1);
                float normalizedPos = fmod(coord, segmentWidth);
                float distToStart = normalizedPos;
                float segmentIndex = floor(coord / segmentWidth);
                float dividerMask = step(distToStart, halfDividerWidth) * step(0.5, segmentIndex);

                
                float2 dividerUV;
                dividerUV.x = lerp(normalizedPos / _DividerWidth, IN.texcoord.x, isVertical);
                dividerUV.y = lerp(IN.texcoord.y, normalizedPos / _DividerWidth, isVertical);
                
                fixed4 divider = tex2D(_DividerTex, dividerUV) * _DividerColor;
                float finalMask = segmentMask * dividerMask;
                
                c = lerp(c, divider, divider.a * finalMask);
                c.rgb *= c.a;
                return c;
            }
            ENDCG
        }
    }
}